﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using isct.escada.domain.core.DeviceDriver;

namespace isct.escada.infraestructure.drivers.opc
{
    public class OpcDriver : domain.core.DeviceDriver.IBasicDriver
    {
        private OPCDA.NET.OpcServer m_Server;

        #region Implementation of IBasicDriver

        public int Connect(Dictionary<string, string> prmConnectionArgs)
        {
            try
            {
                m_Server = new OPCDA.NET.OpcServer();

                if (!prmConnectionArgs.ContainsKey("Server"))
                {
                    throw new BasicDriverException("No se especifico un nombre de servidor");
                }

                if (prmConnectionArgs.ContainsKey("Computer"))
                {
                    var rpta = m_Server.Connect(prmConnectionArgs["Computer"],
                                                prmConnectionArgs["Server"]);
                    if (rpta > 0)
                    {
                        Status = EBasicDriverStatus.Connected;
                    }
                    else
                    {
                        Status = EBasicDriverStatus.Disconnected;
                    }

                    
                }

                return m_Server.Connect(prmConnectionArgs["Server"]);
            }
            catch (Exception ex)
            {
                throw new BasicDriverException("No se pudo conectar con el servidor de comunicacion", ex);
            }

            //Logica de reintentos de coneccion

        }

        public int Disconnect()
        {
            try
            {
                //desconectar a todos los clientes registrados antes de cerrar la conexion con el servidor.
                m_Server.Disconnect();
                return 1;
            }
            catch (Exception ex)
            {
                throw new BasicDriverException("Error de desconexion", ex);
            }
        }

        public EBasicDriverStatus Status { get; set; }

        public IDeviceObjectStatus ReadObject(IDeviceObjectInfo prmDeviceObjectDef)
        {
            throw new NotImplementedException();
        }

        public IWriteError WriteObject(IDeviceObjectInfo prmDeviceObjectDef, IDeviceObjectStatus prmDeviceObjectStatus)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<IDeviceObjectStatus> ReadObjects(IEnumerable<IDeviceObjectInfo> prmDeviceObjectDef)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<IWriteError> WriteObjects(IDictionary<IDeviceObjectInfo, IDeviceObjectStatus> prmDeviceAfectedObjects)
        {
            throw new NotImplementedException();
        }

        public int RegisterObjects(IEnumerable<IDeviceObjectInfo> prmDeviceObjectDef, BasicDriverRegisteredObjectCallbak prmCallback)
        {
            throw new NotImplementedException();
        }

        #endregion
    }


}
